package pt.cp.view;

import org.joda.time.DateTime;
import pt.cp.CP;
import pt.cp.commands.GetTimeTables;
import swing.Dialog;
import swing.EntityTable;
import swing.FormPanel;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;

import static java.awt.BorderLayout.NORTH;
import static java.awt.Color.black;
import static swing.Dialog.Flag.CENTERED;
import static swing.Dialog.Flag.PACKED;

public class CPDialog extends Dialog {

    /**
     *
     */
    private static final long serialVersionUID = -696976186067428423L;

    public CPDialog() {
        super(PACKED, CENTERED);

        getContentPane().add(new JScrollPane(new SearchPanel()));
    }

    private class SearchPanel extends JPanel {
        /**
         *
         */
        private static final long serialVersionUID = 8682907408203708655L;

        private final TimetablesAndPricesPanel timetablesAndPricesPanel = new TimetablesAndPricesPanel();
        private final ResultsPanel resultsPanel = new ResultsPanel();

        private SearchPanel() {
            setLayout(new BorderLayout());


            add(timetablesAndPricesPanel, NORTH);
            add(new JScrollPane(resultsPanel));
        }

        private class TimetablesAndPricesPanel extends FormPanel {
            /**
             *
             */
            private static final long serialVersionUID = 9173750486967382987L;

            private final StationPicker departStationPicker = new StationPicker();
            private final StationPicker arrivalStationPicker = new StationPicker();
            private final DatePicker datePicker = new DatePicker();

            private TimetablesAndPricesPanel() {
                super("Pesquisar");

                addField("Origem", departStationPicker);
                addField("Destino", arrivalStationPicker);
                addField("Data", datePicker);

                defaultButton.addActionListener(e -> new GetTimeTables()
                        .setDepart(departStationPicker.getStation().toString())
                        .setArrival(arrivalStationPicker.getStation().toString())
                        .setDate(datePicker.getText())
                        .setTime("")
                        .setReturnDate("")
                        .setReturnTime("")
                        .setAllServices("allServices")
                        .onFinished((o, arg) -> resultsPanel.stationEntityTable.setEntity((GetTimeTables.Entity) arg))
                        .onAborted((o1, arg) -> popup((Exception) arg))
                        .execute());
            }

            private class DatePicker extends JTextField {
                /**
                 *
                 */
                private static final long serialVersionUID = 1334094797043888665L;

                private DatePicker() {
                    super(CP.DATE_TIME_FORMATTER.print(new DateTime()));
                }
            }

        }

        private class ResultsPanel extends JPanel {
            /**
             *
             */
            private static final long serialVersionUID = 1724027642407100944L;

            private final EntityTable<Integer, GetTimeTables.Item> stationEntityTable = new EntityTable<>(new GetTimeTables.Entity());

            private ResultsPanel() {
                setBorder(new TitledBorder(new LineBorder(black), "Resultados"));
                add(stationEntityTable);
            }
        }
    }
}
